<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

class Loader
{
    //已包含的文件
    static protected $_configs = array();
    static protected $_langs = array();

    static protected function _loadFile($name, $dir, $mod = '', $type = '') {
        //如不指定$mod，则首先搜索当前模块，再搜索LIB_ROOT。否则，搜索指定模块
        if('' == $mod) {
            $mod = 'wogu';
            $searchPathes = array();
            $request = Register::get('Request', 'WOGU_SYS');
            if($request && $request->get_module()) {
                $searchPathes[] = LIB_ROOT . 'modules' . DIRECTORY_SEPARATOR . $request->get_module() . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
            }

            $searchPathes[] = LIB_ROOT . $dir . DIRECTORY_SEPARATOR;
        } else {
            $searchPathes[] = LIB_ROOT . 'modules' . DIRECTORY_SEPARATOR . $mod . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
        }

        $filename = str_replace('_', DIRECTORY_SEPARATOR, $name) . ($type == '' ? '' : ('.' . $type)) . '.php';

        foreach($searchPathes as $sp) {
            $fullpath = $sp . $filename;
            if(file_exists($fullpath)) {
                return require_once $fullpath;
            }
        }

        return false;
    }

    static protected function _loadClass($name, $dir, $mod, $init) {
        if(!self::_loadFile($name, $dir, $mod, 'class')) {
            return false;
        }

        if($init) {
            return new $name;
        }

        return true;
    }

    static public function loadClass($name, $init = false) {
        $dir = 'libs' . DIRECTORY_SEPARATOR . 'classes';
        return self::_loadClass($name, $dir, '', $init);
    }
    
    static public function loadUserClass($name, $init = false, $mod = '') {
        return self::_loadClass($name, 'classes', $mod, $init);
    }

    static public function loadModel($name, $init = false) {
        $dir = 'libs' . DIRECTORY_SEPARATOR . 'models';
        return self::_loadClass($name, $dir, '', $init);
    }
    
    static public function loadUserModel($name, $init = false, $mod = '') {
        return self::_loadClass($name, 'models', $mod, $init);
    }

    static public function loadPlugin($name, $init = false, $mod = '') {
        return self::_loadClass($name, 'plugins', $mod, $init);
    }

    static public function loadFunc($name) {
        $dir = 'libs' . DIRECTORY_SEPARATOR . 'functions';
        return self::_loadFile($name, $dir, '', 'func');
    }
    
    static public function loadUserFunc($name, $mod = '') {
        return self::_loadFile($name, 'functions', $mod, 'func');
    }

    static public function loadConfig($name, $mod = '') {
        $module = $mod == '' ? 'wogu' : $mod;

        if(isset(self::$_configs[$module][$name])) {
            return self::$_configs[$module][$name];
        }

        $result = self::_loadFile($name, 'configs', $mod, 'inc');
        self::$_configs[$module][$name] = $result;

        return $result;
    }

    static public function loadLang($name, $mod = '') {
        $module = $mod == '' ? 'wogu' : $mod;

        if(isset(self::$_langs[$module][$name])) {
            return self::$_langs[$module][$name];
        }

        $result = self::_loadFile($name, 'lang', $mod, 'lang');
        self::$_langs[$module][$name] = $result;

        return $result;
    }
    
    static public function loadThirdPart($name, $type = '', $dir = '') {
        $dir = 'thirdpart' . ($dir == '' ? '' : '/' . $dir);
        return self::_loadFile($name, $dir, '', $type);
    }
}